package com.gwm.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwm.common.enums.ExceptionEnums;
import com.gwm.common.exceptions.LyException;
import com.gwm.common.utils.BeanHelper;
import com.gwm.item.dto.CategoryDTO;

import com.gwm.item.service.TbCategoryService;
import com.gwm.item.mapper.TbCategoryMapper;
import com.gwm.item.pojo.TbCategory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 服务实现类
 * </p>
 *
 * @author heima
 * @since 2020-12-01
 */
@Service
public class TbCategoryServiceImpl extends ServiceImpl<TbCategoryMapper, TbCategory> implements TbCategoryService {

    @Override
    public List<CategoryDTO> findCategoryListByParentId(Long pid) {
//        select * from tb_category where parent_id=?
        QueryWrapper<TbCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbCategory::getParentId,pid);
        List<TbCategory> tbCategoryList = this.list(queryWrapper);

        if(CollectionUtils.isEmpty(tbCategoryList)){
            throw new LyException(ExceptionEnums.CATEGORY_NOT_FOUND);
        }
        List<CategoryDTO> categoryDTOList = BeanHelper.copyWithCollection(tbCategoryList, CategoryDTO.class);
        return categoryDTOList;
    }

    @Override
    public List<CategoryDTO> findCategoryListByBrandId(Long brandId) {
        List<TbCategory> tbCategoryList  = this.getBaseMapper().findCategoryListByBrandId(brandId);
        if(CollectionUtils.isEmpty(tbCategoryList)){
            throw new LyException(ExceptionEnums.CATEGORY_NOT_FOUND);
        }
        List<CategoryDTO> categoryDTOList = BeanHelper.copyWithCollection(tbCategoryList, CategoryDTO.class);
        return categoryDTOList;
    }
}
